package com.united.ucts.modules.system.controller;

import com.united.frame.common.module.result.PageResult;
import com.united.frame.common.module.result.Result;
import com.united.ucts.modules.system.pojo.po.Menu;
import com.united.ucts.modules.system.pojo.po.Resource;
import com.united.ucts.modules.system.pojo.po.Role;
import com.united.ucts.modules.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 后台用户角色管理
 *
 * @author Hudan
 * @version 1.0
 * @filename RoleController.java
 * @copyright <a href="www.united.cn">www.united.cn</a>
 * @date 2019-08-16
 */
@Api(tags = "RoleController", value = "后台用户角色管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping(value = "/create")
    public Result<Integer> create(@Valid @RequestBody Role role) {
        int count = roleService.create(role);
        if (count > 0) {
            return Result.success(count);
        }
        return Result.failure();
    }

    @ApiOperation("批量删除角色")
    @PostMapping(value = "/delete")
    public Result<Integer> delete(@RequestParam("ids") List<String> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return Result.success(count);
        }
        return Result.failure();
    }

    @ApiOperation("修改角色")
    @PostMapping(value = "/update/{id}")
    public Result<Integer> update(@PathVariable String id, @Valid @RequestBody Role role) {
        int count = roleService.update(id, role);
        if (count > 0) {
            return Result.success(count);
        }
        return Result.failure();
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @GetMapping(value = "/list")
    public Result<PageResult<Role>> pageBy(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           @RequestParam(value = "keyword", required = false) String keyword) {
        List<Role> roles = roleService.pageBy(pageNum, pageSize, keyword);
        return Result.success(PageResult.restPage(roles));
    }

    @ApiOperation("获取所有角色")
    @GetMapping(value = "/listAll")
    public Result<List<Role>> listAll() {
        List<Role> roleList = roleService.list();
        return Result.success(roleList);
    }

    @ApiOperation("修改角色状态")
    @PostMapping(value = "/updateStatus/{id}")
    public Result<Integer> updateStatus(@PathVariable String id, @RequestParam(value = "status") Integer status) {
        Role role = new Role();
        role.setStatus(status);
        int count = roleService.update(id, role);
        if (count > 0) {
            return Result.success(count);
        }
        return Result.failure();
    }

    @ApiOperation("给角色分配菜单")
    @PostMapping(value = "/allocMenu")
    public Result<Integer> allocMenu(@RequestParam String roleId, @RequestParam List<String> menuIds) {
        int count = roleService.allocMenu(roleId, menuIds);
        return Result.success(count);
    }

    @ApiOperation("给角色分配资源")
    @PostMapping(value = "/allocResource")
    public Result<Integer> allocResource(@RequestParam String roleId, @RequestParam List<String> resourceIds) {
        int count = roleService.allocResource(roleId, resourceIds);
        return Result.success(count);
    }

    @ApiOperation("获取角色相关菜单")
    @GetMapping(value = "/listMenu/{roleId}")
    public Result<List<Menu>> listMenu(@PathVariable String roleId) {
        List<Menu> roleList = roleService.listMenu(roleId);
        return Result.success(roleList);
    }

    @ApiOperation("获取角色相关资源")
    @GetMapping(value = "/listResource/{roleId}")
    public Result<List<Resource>> listResource(@PathVariable String roleId) {
        List<Resource> roleList = roleService.listResource(roleId);
        return Result.success(roleList);
    }
}